package com.wd.elec.controller.bussiness;

import java.util.Calendar;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.wd.elec.service.impl.AttendCalculateService;
import org.springframework.security.access.prepost.PreAuthorize;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wd.common.core.controller.BaseController;
import com.wd.common.core.domain.HttpResponse;
import com.wd.common.annotation.Log;
import com.wd.common.enums.BusinessType;
import com.wd.elec.domain.AttendResult;
import com.wd.elec.service.IAttendResultService;
import com.wd.common.utils.poi.ExcelUtil;


/**
 * 考勤结果Controller
 *
 * @author liumingkun
 * @date 2023-11-16
 */
@RestController
@RequestMapping("/attend/result")
public class AttendResultController extends BaseController {
    @Resource
    private IAttendResultService attendResultService;

    @Resource
    private AttendCalculateService calculateService;

    /**
     * 查询考勤结果列表
     */
    @PreAuthorize("@ss.hasPermi('attend:result:list')")
    @GetMapping("/list")
    public HttpResponse list(AttendResult attendResult) {
        startPage();
        List<AttendResult> list = attendResultService.selectAttendResultList(attendResult);
        return HttpResponse.successData(getDataTable(list));
    }

    /**
     * 导出考勤结果列表
     */
    @PreAuthorize("@ss.hasPermi('attend:result:export')")
    @Log(title = "考勤结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AttendResult attendResult) {
        List<AttendResult> list = attendResultService.selectAttendResultList(attendResult);
        ExcelUtil<AttendResult> util = new ExcelUtil<AttendResult>(AttendResult.class);
        util.exportExcel(response, list, "考勤结果数据");
    }

    /**
     * 获取考勤结果详细信息
     */
    @PreAuthorize("@ss.hasPermi('attend:result:query')")
    @GetMapping(value = "/{resultId}")
    public HttpResponse getInfo(@PathVariable("resultId") Long resultId) {
        return successData(attendResultService.selectAttendResultByResultId(resultId));
    }

    /**
     * 新增考勤结果
     */
    @PreAuthorize("@ss.hasPermi('attend:result:add')")
    @Log(title = "考勤结果", businessType = BusinessType.INSERT)
    @PostMapping
    public HttpResponse add(@RequestBody AttendResult attendResult) {
        return getResult(attendResultService.insertAttendResult(attendResult));
    }

    /**
     * 修改考勤结果
     */
    @PreAuthorize("@ss.hasPermi('attend:result:edit')")
    @Log(title = "考勤结果", businessType = BusinessType.UPDATE)
    @PutMapping
    public HttpResponse edit(@RequestBody AttendResult attendResult) {
        return getResult(attendResultService.updateAttendResult(attendResult));
    }

    /**
     * 删除考勤结果
     */
    @PreAuthorize("@ss.hasPermi('attend:result:remove')")
    @Log(title = "考勤结果", businessType = BusinessType.DELETE)
    @DeleteMapping("/{resultIds}")
    public HttpResponse remove(@PathVariable Long[] resultIds) {
        return getResult(attendResultService.deleteAttendResultByResultIds(resultIds));
    }


    @RequestMapping("/job")
    public HttpResponse attendReportJob(Integer year, Integer month, Integer day) {

        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day, 0, 0, 0);
        calculateService.calculateAttend(calendar.getTime());
        return HttpResponse.success();
    }
}
